<?php

namespace App\Modules\LuckDraw\Message;

use App\Modules\LuckDraw\Lib\StrLib;
use App\Modules\LuckDraw\Models\MessageModel;

class MessageFactory {
    private static $msgInstances = [];
    //获取一个消息实例
    public static function get(int $msgId) : ?MessageInterface {
        if (empty($msgId)) {
            return null;
        }
        if (isset(self::$msgInstances[$msgId])) {
            return self::$msgInstances[$msgId];
        }
        $messageModel = MessageModel::get($msgId);
        $className = __NAMESPACE__ . '\\' . StrLib::camelize($messageModel->en_name) . 'Message';
        if(!class_exists($className)){
            return null;
        }

        $messageInstance = new $className($messageModel);
        self::$msgInstances[$msgId] = $messageInstance;
        return $messageInstance;
    }
}